﻿using Face8280WebSocketServer.DB.Device;
using Face8280WebSocketServer.DB.TimeGroup;
using Face8280WebSocketServer.Interface;
using Microsoft.EntityFrameworkCore;
using System.Collections.Generic;
using System.Linq;

namespace Face8280WebSocketServer.Service
{
    /// <summary>
    /// 开门时段
    /// </summary>
    public class TimeGroupService : BaseService, ITimeGroupService
    {
        ICacheService _CacheService;

        public TimeGroupService(DbContext context, ICacheService cache) : base(context)
        {
            _CacheService = cache;
        }


        public void IniTimeGroupDB()
        {
            Excute("Delete from TimeGroup");
            List<TimeGroupDetail> lst = new List<TimeGroupDetail>();

            for (int i = 0; i < 64; i++)
            {
                lst.Add(new TimeGroupDetail(i + 1));

            }
            var detail = lst[0];
            detail.Week1 = "00:00--23:59/00:00--00:00/00:00--00:00/00:00--00:00/00:00--00:00/00:00--00:00/00:00--00:00/00:00--00:00";
            detail.Week2 = detail.Week1;
            detail.Week3 = detail.Week1;
            detail.Week4 = detail.Week1;
            detail.Week5 = detail.Week1;
            detail.Week6 = detail.Week1;
            detail.Week7 = detail.Week1;

            DetachAll();
            Insert<TimeGroupDetail>(lst);

            UpdateAllDevice();

        }

        /// <summary>
        /// 更新所有设备的待上传状态
        /// </summary>
        private void UpdateAllDevice()
        {
            //Excute("Update FaceDevice set UploadStatus = 0;");
            //var devicequrey = from d in Set<FaceDeviceDetail>()
            //                  select d.DeviceID;
            //var device = _CacheService.GetDevices();
            //foreach (var d in device)
            //{
            //   d.up
            //}
           

        }

        public List<TimeGroupDetail> GetAll()
        {
            List<TimeGroupDetail> lst = Query<TimeGroupDetail>(p => true).OrderBy(x => x.Num).ToList();
            return lst;
        }

        public void UpdateTimeGroup(TimeGroupDetail detail)
        {
            Update(detail);
            UpdateAllDevice();
        }
    }
}
